package test;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

class TableDemo11 extends JFrame
{
  TableDemo11 (String title)
  {
   // Pass the title to the JFrame superclass so that it appears in
   // the title bar.

   super (title);

   // Tell the program to exit when the user either selects Close
   // from the System menu or presses an appropriate X button on the
   // title bar.

   setDefaultCloseOperation (EXIT_ON_CLOSE);

   // Create a default table model consisting of 4 rows by 3 
   // columns.

   DefaultTableModel dtm = new DefaultTableModel (4, 3);

   // Assign column identifiers (headers) to the columns.

   String [] columnTitles =
   {
     "Name",
     "Address",
     "Favorite Color",
   };

   dtm.setColumnIdentifiers (columnTitles);

   // Populate all cells in the default table model.

   String [] names =
   {
     "John Doe",
     "Jane Smith",
     "Jack Jones",
     "Paul Finch"
   };

   String [] addresses =
   {
     "200 Fox Street",
     "Apt. 555",
     "Box 9000",
     "1888 Apple Avenue"
   };

   String [] favoriteColors =
   {
     "red",
     "blue",
     "green",
     "yellow"
   };

   int nrows = dtm.getRowCount ();

   for (int i = 0; i < nrows; i++)
   {
      dtm.setValueAt (names [i], i, 0);
      dtm.setValueAt (addresses [i], i, 1);
      dtm.setValueAt (favoriteColors [i], i, 2);
   }

   // Create a table using the previously created default table
   // model.

   JTable jt = new JTable (dtm);

   // Establish a color renderer for the third column.

   jt.getColumnModel ().getColumn (2).
              setCellRenderer (new ColorRenderer());

   // Place the table in a JScrollPane object (to allow the table to
   // be vertically scrolled and display scrollbars, as necessary).

   JScrollPane jsp = new JScrollPane (jt);

   // Add the JScrollPane object to the frame window's content pane.
   // That allows the table to be displayed within a displayed scroll
   // pane.

   getContentPane ().add (jsp);

   // Establish the overall size of the frame window to 400
   // horizontal pixels by 110 vertical pixels.

   setSize (400, 110);

   // Display the frame window and all contained
   // components/containers.

   setVisible (true);
  }

  public static void main (String [] args)
  {
   // Create a TableDemo11 object, which creates the GUI.

   new TableDemo11 ("Table Demo #11");
  }
}

class ColorRenderer extends JLabel implements TableCellRenderer
{
  ColorRenderer ()
  {
   // Ensure that all nonforeground pixels are painted in the
   // background.
   // color.

   setOpaque (true);

   // Switch off JLabel's use of BOLD.

   setFont (getFont ().deriveFont (Font.PLAIN));
  }

  public Component getTableCellRendererComponent (JTable table,
                          Object value,
                          boolean isSelected,
                          boolean isFocus,
                          int row,
                          int column)
  {
   // As a safety check, it's always good to verify the type of
   // value.

   if (value instanceof String)
   {
     String s = (String) value;

     // Establish an appropriate background color. That color
     // remains in effect for all columns to which the
     // ColorRenderer object
     // is attached until setBackground(Color c) is called again.

     if (s.equals ("red"))
       setBackground (Color.red);
     else
     if (s.equals ("green"))
       setBackground (Color.green);
     else
     if (s.equals ("blue"))
       setBackground (Color.blue);
     else
     if (s.equals ("yellow"))
       setBackground (Color.yellow);
     else
       setBackground (Color.white);

     // Ensure text is displayed horizontally.

     setHorizontalAlignment (CENTER);

     // Assign the text to the JLabel component.

     setText (s);
   }

   // Return a reference to the ColorRenderer (JLabel subclass)
   // component. Behind the scenes, that component will be used to
   // paint the pixels.

   return this;
  }
}